<template>
    <div id="monthWorkList">
      <el-dialog title="用户信息" :visible.sync="dialogFormVisible">
        <el-form :model="form" :rules="rules" ref="form">
          <el-form-item label="姓名" prop="name" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="登录名" prop="username" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input v-model="form.username" :disabled="codeBtype" autocomplete="off"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="职位" prop="position" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input v-model="form.position" autocomplete="off"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="设置密码" prop="newPassword"  :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input type="password" v-model="form.newPassword" autocomplete="off"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword"  :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input type="password" v-model="form.confirmPassword" autocomplete="off"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="备注" prop="node" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input v-model="form.node" autocomplete="off"></el-input>
            </el-col>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="formSubmit('form')">确 定</el-button>
        </div>
      </el-dialog>
      <el-dialog title="分配角色" :visible.sync="dialogAllotVisible">
        <el-form :model="formModel" :rules="rulesModel" ref="formModel">
          <el-form-item label="菜单角色" prop="valueRole1" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-select clearable v-model="formModel.valueRole1" placeholder="请选择菜单角色">
                <el-option
                  v-for="item in roleList1"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="数据角色" prop="valueRole2" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-select clearable v-model="formModel.valueRole2" placeholder="请选择数据角色">
                <el-option
                  v-for="item in roleList2"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="操作角色" prop="valueRole3" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-select clearable v-model="formModel.valueRole3" placeholder="请选择操作角色">
                <el-option
                  v-for="item in roleList3"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-col>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogAllotVisible = false">取 消</el-button>
          <el-button type="primary" @click="allotSubmit('formModel')">确 定</el-button>
        </div>
      </el-dialog>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="姓名">
          <el-input v-model="formInline.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item label="登录名">
          <el-input v-model="formInline.loginName" placeholder="登录名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="success" @click="add">新增</el-button>
        </el-form-item>
      </el-form>
      <el-table
          :data="tableData"
          border
          style="width: 100%">
          <el-table-column
            prop="createTime"
            label="创建时间"
            align="center"
            width="180">
          </el-table-column>
          <el-table-column
            prop="username"
            label="登录名"
            align="center"
            width="180">
          </el-table-column>
          <el-table-column
            prop="name"
            label="姓名"
            align="center"
            width="180">
          </el-table-column>
          <el-table-column
            prop="position"
            label="职位"
            align="center">
          </el-table-column>
          <!-- <el-table-column
            prop="projectNames"
            label="所属项目组"
            align="center">
          </el-table-column> -->
          <el-table-column
            prop="node"
            label="备注"
            align="center">
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="300">
            <template slot-scope="scope">
              <el-button @click="handleClick(scope.row)" type="text" size="small">修改</el-button>
              <el-button @click.native.prevent="deleteRow(scope.$index, tableData)" type="text" size="small">删除</el-button>
              <el-button @click="handleAllot(scope.row)" type="text" size="small">分配角色</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[15, 25, 50]"
            :page-size="perPage"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
        <el-input v-model="rowId" type="hidden"></el-input>
    </div>
</template>
<script>
export default {
  name: 'monthWorkList',
  data () {
    return {
      formInline: {
        name: '',
        loginName: ''
      },
      form: {
        name: '',
        username: '',
        position: '',
        newPassword:'',
        confirmPassword:'',
        node: ''
      },
      rulesModel: {
        valueRole1: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        valueRole2: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        valueRole3: [
          { required: true, message: '请选择', trigger: 'change' }
        ]
      },
      rules: {
        name: [
          { required: true, message: '请填写姓名', trigger: 'blur' }
        ],
        username: [
          { required: true, message: '请填写登录名', trigger: 'blur' }
        ],
        position: [
          { required: true, message: '请填写职位', trigger: 'blur' }
        ]
        // ,
        // newPassword: [
        //   { required: true, message: '请填写新密码', trigger: 'blur' }
        // ],
        // confirmPassword: [
        //   { required: true, message: '请确认密码', trigger: 'blur' }
        // ]
        // ,
        // node: [
        //   { required: true, message: '请填写备注', trigger: 'blur' }
        // ]
      },
      roleList1:[],
      roleList2:[],
      roleList3:[],
      formModel: {
        valueRole1:'',
        valueRole2:'',
        valueRole3:''
      },     
      operaType:'',
      rowId:'',
      codeBtype:false,
      formLabelWidth: '320px',
      dialogFormVisible:false,
      dialogAllotVisible:false,
      currentPage: 1,
      perPage: 15,
      total: null,
      tableData: []
    };
  },
  created() {
    //调用加载表格方法
    this.getTableList();
  },
  beforeDestroy() {},
  computed: {},
  mounted() {},
  methods: {
    onSubmit() {
      console.log('submit!');
      var self = this;
      self.getTableList();
    },
    handleAllot(row){ 
      var self = this;
      self.dialogAllotVisible = true;
      self.rowId = row.id;
      //console.log(row)
      //查询角色
      self.
        postRequest("/user/isAllotRoles", {
          id:self.rowId
        }).then(
          res => {      
            console.log(res.data) 
            var obj = res.data.allRoles;
            var obj2 = res.data.hasRoles;
            var arr1 = [];
            var arr2 = [];
            var arr3 = [];
            //var row = {};
            for(var i in obj2){
              if (parseInt(obj2[i].rType) == 1){
               self.formModel.valueRole1 = obj2[i].id;
              }else if(parseInt(obj2[i].rType) == 2){
                self.formModel.valueRole2 = obj2[i].id;
              }else if(parseInt(obj2[i].rType) == 3){
                self.formModel.valueRole3 = obj2[i].id;
              }
            }
            for(var i in obj) {
              var row1 = {};
              var row2 = {};
              var row3 = {};
              if (parseInt(obj[i].rType) == 1){
                row1.label = obj[i].rName;
                row1.value = obj[i].id;
                arr1.push(row1);
              }else if(parseInt(obj[i].rType) == 2){
                row2.label = obj[i].rName;
                row2.value = obj[i].id;
                arr2.push(row2);
              }else if(parseInt(obj[i].rType) == 3){
                row3.label = obj[i].rName;
                row3.value = obj[i].id;
                arr3.push(row3);
              }
              
              
            }
            //console.log(arr)
            self.roleList1 = arr1;
            self.roleList2 = arr2;
            self.roleList3 = arr3;

          }
        );
    },
    allotSubmit(formName) {
      this.$refs[formName].validate((valid) => {
          if (valid) {
            var self = this;
            var valueRoleList = [];
            valueRoleList.push(self.formModel.valueRole1);
            valueRoleList.push(self.formModel.valueRole2);
            valueRoleList.push(self.formModel.valueRole3);
            console.log(valueRoleList)
            self.
              postRequest("/user/allotRoles", {
                id:self.rowId,
                roleIds:valueRoleList
              }).then(
                res => {      
                  //console.log(res) 
                  if(res.data.result == '1' ){            
                    this.$message({
                        message: res.data.message,
                        type: 'success'
                    });
                  }else if(res.data.result == '2' ){
                    this.$message({
                        message: res.data.message,
                        type: 'error'
                    });
                  }   
                  this.dialogAllotVisible = false;
                  self.getTableList();
                }
              );
          }
      })
      
    },
    //新增
    add() {
      var self = this;
      self.dialogFormVisible = true;
      self.operaType = 'add';
      self.codeBtype = false;
      self.form.name = '';
      self.form.username = '';
      self.form.position = '';
      self.form.node = '';
    },
    //修改
    handleClick(row) {
      console.log(row);
      var self = this;
      self.rowId = row.id;
      self.operaType = 'update';
      self.codeBtype = true;
      self.dialogFormVisible = true;
      //console.log(thselfis.form)
      self.form.name = row.name;
      self.form.username = row.username;
      self.form.position = row.position;
      self.form.node = row.node;
      
    },
    //确定新增项目
    formSubmit(formName) {
      //console.log('submit!');   
      this.$refs[formName].validate((valid) => {
          if (valid) {
            var self = this;
            if(self.form.newPassword !== self.form.confirmPassword){
              this.$message({
                  message: '两次密码不一致',
                  type: 'error'
              });
              return;
            }
            console.log(self.operaType)
            if(self.operaType == 'add'){
              self.
                postRequest("/user/insert", {
                  name:self.form.name,
                  username:self.form.username,
                  position:self.form.position,
                  password:self.form.newPassword,
                  node:self.form.node
                }).then(
                  res => {      
                    console.log(res) 
                    if(res.data.result == '1' ){            
                      this.$message({
                          message: res.data.message,
                          type: 'success'
                      });
                    }else if(res.data.result == '2' ){
                      this.$message({
                          message: res.data.message,
                          type: 'error'
                      });
                    }else if(res.data.result == '-2' ){
                      this.$message({
                          message: res.data.message,
                          type: 'error'
                      });
                    }    
                    this.dialogFormVisible = false;
                    self.getTableList();
                  }
                );
            }else if(self.operaType == 'update'){
              //console.log(self.rowId)
              self.
                postRequest("/user/update", {
                  name:self.form.name,
                  username:self.form.username,
                  position:self.form.position,
                  password:self.form.newPassword,
                  id:self.rowId,
                  node:self.form.node
                }).then(
                  res => {      
                    //console.log(res) 
                    if(res.data.result == '1' ){            
                      this.$message({
                          message: res.data.message,
                          type: 'success'
                      });
                    }else if(res.data.result == '2' ){
                      this.$message({
                          message: res.data.message,
                          type: 'error'
                      });
                    }else if(res.data.result == '-2' ){
                      this.$message({
                          message: res.data.message,
                          type: 'error'
                      });
                    }    
                    this.dialogFormVisible = false;
                    self.getTableList();
                    self.codeBtype = false;
                  }
                );
            }
          }
      })   
      
      
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      var self = this;
      self.perPage = val;
      self.getTableList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      var self = this;
      self.currentPage = val;
      self.getTableList();
    },
    deleteRow(index, rows) {
      console.log(rows[index])
      var self = this;
      self.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          self.
            postRequest("/user/delete", {
              id:rows[index].id
            }).then(
              res => {      
                console.log(res.data) 
                if(res.data.result == '1' ){            
                  this.$message({
                      message: res.data.message,
                      type: 'success'
                  });
                }else if(res.data.result == '2' ){
                  this.$message({
                      message: res.data.message,
                      type: 'error'
                  });
                }    
                self.getTableList();
              }
            );
        }).catch(() => {
          self.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    //表格加载
    getTableList() {
      var self = this;
      //console.log(self.currentPage)
      self.getRequest("/user/queryByPage?page_no=" +self.currentPage+'&&page_size='+self.perPage
       +'&&name='+self.formInline.name+'&&loginName='+self.formInline.loginName
      , {}).then(
        res => {      
          console.log(res.data)     
          self.tableData = res.data.data;
          self.total = res.data.recordsTotal;
          self.perPage = res.data.length;
        }
      );
    }
  }
}
</script>

<style scoped>
  .demo-form-inline {text-align: left;}
  .el-pagination {margin: 20px 0;}
</style>